package com.nmgs.util;

import javax.xml.ws.Holder;
import java.util.List;
import java.util.Map;

public class GetSERVERINFOUtil {
    public static void getSERVERINFO(Map<String, Object> params, String name, Holder<String> ip, Holder<String> port) {
        if (params.get("SERVERINFO") != null &&
                !params.get("SERVERINFO").toString().trim().equals("") &&
                !params.get("SERVERINFO").toString().trim().equals("[]")
        ) {
            try {
                List<Map> list = com.alibaba.fastjson.JSONObject.parseObject(params.get("SERVERINFO").toString(), List.class);
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).get("NAME").toString().equals(name)) {
                            ip.value = list.get(i).get("IP").toString();
                            port.value = list.get(i).get("PORT").toString();
                            break;
                        }
                        if (i == list.size() - 1) {
                            LogUtil.error("数据库NAME_TABLE_DCOM未配置staNo=" + (params.get("STANO") == null ? "" : params.get("STANO").toString()) + "的SERVERINFO不存在" + name + "的信息", GetSERVERINFOUtil.class);
                        }
                    }
                } else {
                    LogUtil.error("数据库NAME_TABLE_DCOM未配置STANO=" + (params.get("STANO") == null ? "" : params.get("STANO").toString()) + "的SERVERINFO不存在" + name + "的信息", GetSERVERINFOUtil.class);
                }
            } catch (Exception e) {
                LogUtil.error("数据库NAME_TABLE_DCOM未配置STANO=" + (params.get("STANO") == null ? "" : params.get("STANO").toString()) + "的上级的SERVERINFO格式错误或不存在" + name + "的信息", GetSERVERINFOUtil.class);
            }
        } else {
            LogUtil.error("数据库NAME_TABLE_DCOM未配置STANO=" + (params.get("STANO") == null ? "" : params.get("STANO").toString()) + "的上级的SERVERINFO格式错误或不存在" + name + "的信息", GetSERVERINFOUtil.class);
        }
    }

    public static void getSERVERINFO(Map<String, Object> params, Holder<String> sshPort, Holder<String> userName, Holder<String> passWord) {
        if (params.get("SERVERINFO") != null &&
                !params.get("SERVERINFO").toString().trim().equals("") &&
                !params.get("SERVERINFO").toString().trim().equals("[]")
        ) {
            try {
                List<Map> list = com.alibaba.fastjson.JSONObject.parseObject(params.get("SERVERINFO").toString(), List.class);
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        int breakNum = 0;
                        if (list.get(i).get("NAME").toString().equals("SSHINFO")) {
                            sshPort.value = list.get(i).get("PORT").toString();
                            if (list.get(i).get("NAMECN") != null && list.get(i).get("NAMECN").toString().contains("=")) {
                                String str = list.get(i).get("NAMECN").toString();
                                userName.value = str.substring(0, str.indexOf("=", 1));
                                passWord.value = str.substring(str.indexOf("=", 1) + 1);
                            } else {
                                LogUtil.error("数据库NAME_TABLE_DCOM未配置staNo=" + (params.get("STANO") == null ? "" : params.get("STANO").toString()) + "的SERVERINFO不存在SSHINFO的信息", GetSERVERINFOUtil.class);
                            }
                            break;
                        }
                        if (i == list.size() - 1) {
                            LogUtil.error("数据库NAME_TABLE_DCOM未配置staNo=" + (params.get("STANO") == null ? "" : params.get("STANO").toString()) + "的SERVERINFO不存在SSHINFO的信息", GetSERVERINFOUtil.class);
                        }
                    }
                } else {
                    LogUtil.error("数据库NAME_TABLE_DCOM未配置STANO=" + (params.get("STANO") == null ? "" : params.get("STANO").toString()) + "的SERVERINFO不存在SSHINFO的信息", GetSERVERINFOUtil.class);
                }
            } catch (Exception e) {
                LogUtil.error("数据库NAME_TABLE_DCOM未配置STANO=" + (params.get("STANO") == null ? "" : params.get("STANO").toString()) + "的上级的SERVERINFO格式错误或不存在SSHINFO的信息", GetSERVERINFOUtil.class);
            }
        } else {
            LogUtil.error("数据库NAME_TABLE_DCOM未配置STANO=" + (params.get("STANO") == null ? "" : params.get("STANO").toString()) + "的上级的SERVERINFO格式错误或不存在SSHINFO的信息", GetSERVERINFOUtil.class);
        }
    }
}
